<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <script src="../../lib/layui-v2.8.12/src/layui.js"></script>
  <link rel="stylesheet" href="../../lib/layui-v2.8.12/src/css/layui.css">
  <script src="../../utils/form-serialize.js"></script>
  <script src="../../lib/axios/axios.min.js"></script>
  <link rel="stylesheet" href="../../css/common.css">
  <title>Document</title>
</head>

<body>
  <!-- 给容器追加 class="layui-form-pane"，即可显示为方框风格 -->
  <form class="layui-form layui-form-pane" id="add" action="./addUser.php" method="POST">
    <div class="layui-form-item">
      <div class="layui-inline">
        <label class="layui-form-label">用户名</label>
        <div class="layui-input-block" style="width: 200px;">
          <input type="text" name="userName" maxlength="10" minlength="6" autocomplete="off" class="layui-input" required>
        </div>
      </div>
      <div class="layui-inline">
        <label class="layui-form-label">用户密码</label>
        <div class="layui-input-inline" style="width: 200px;">
          <input type="text" name="userPassword" autocomplete="off" class="layui-input" required>
        </div>
      </div>
    </div>

    <div class="layui-form-item">
      <div class="layui-inline">
        <label class="layui-form-label">用户昵称</label>
        <div class="layui-input-inline">
          <input type="text" name="nikeName" autocomplete="off" class="layui-input" required>
        </div>
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">支持球队</label>
      <div class="layui-input-block" style="width: 200px;">
        <select id="selectTeam" name="teamId">
        </select>
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">性别</label>
      <input type="radio" name="gender" value="1" lay-filter="demo-radio-filter" checked>
      <div lay-radio>
        <span style="color: blue;">男 ♂</span>
      </div>
      <input type="radio" name="gender" value="2" lay-filter="demo-radio-filter">
      <div lay-radio>
        <span style="color: pink;">女 ♀</span>
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">生日</label>
      <div class="layui-input-block" style="width: 200px;">
        <input type="text" name="birthday" class="layui-input" id="date" placeholder="yyyy-MM-dd">
      </div>
    </div>

    <div class="layui-form-item">
      <div class="layui-inline">
        <label class="layui-form-label">年龄</label>
        <div class="layui-input-inline" style="width: 100px;">
          <input name="age" autocomplete="off" class="layui-input" required>
        </div>
      </div>
      <div class="layui-inline">
        <label class="layui-form-label">手机</label>
        <div class="layui-input-inline" style="width: 200px;">
          <input name="phone" autocomplete="off" class="layui-input" required>
        </div>
      </div>
      <div class="layui-inline">
        <label class="layui-form-label">地址</label>
        <div class="layui-input-inline" style="width: 150px;">
          <select id="selectAddress" name="address" lay-filter="address">
          </select>
        </div>
      </div>
    </div>


    <div class="layui-form-item btn">
      <div>
        <button class="layui-btn" id="confirm">确认</button>
        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
      </div>
    </div>
  </form>
  <?php
  session_start();
  $user_id = $_SESSION['userId'];
  DEFINE('DB_USER', 'worldcup');
  DEFINE('DB_PASSWORD', '123456');
  DEFINE('DB_HOST', 'localhost');
  DEFINE('DB_NAME', 'worldcup');

  $dbc = @mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
  if (!$dbc) {
    die('Could not to connect to Mysql:' . mysqli_connect_error());
  }
  mysqli_set_charset($dbc, 'utf8');

  $teamNameSql = "select team_name from team;";
  $teamAaary = mysqli_query($dbc, $teamNameSql);

  // 初始化一个空数组来存储所有行数据
  $dataArray = array();
  // 逐行提取数据并存入数组
  while ($row = mysqli_fetch_assoc($teamAaary)) {
    $dataArray[] = $row['team_name'];
  }
  ?>
  <script>
    //支持球队的数据
    let res = <?php echo json_encode($dataArray); ?>;
    var select = document.getElementById('selectTeam');
    for (var i = 1; i <= res.length; i++) {
      var option = document.createElement("option");
      option.setAttribute("value", i);
      option.innerText = res[i - 1];
      if (i == res.length) {
        option.setAttribute("selected", "selected");
      }
      select.appendChild(option)
    }
    let teamId = select.value;
    //地区的数据
    addressData = ["北京", "天津", "上海", "重庆", "河北", "河南", "云南", "辽宁", "黑龙江", "湖南", "安徽", "山东", "新疆", "江苏", "浙江", "江西", "湖北", "广西", "甘肃", "山西", "内蒙古", "陕西", "吉林", "福建", "贵州", "广东", "青海", "西藏", "四川", "宁夏", "海南", "台湾", "香港", "澳门"]
    var selectAddress = document.getElementById('selectAddress');
    for (var i = 1; i <= addressData.length; i++) {
      var options = document.createElement("option");
      options.setAttribute("value", addressData[i - 1]);
      options.innerText = addressData[i - 1];
      if (i == addressData.length) {
        options.setAttribute("selected", "selected");
      }
      selectAddress.appendChild(options)
    }

    layui.use(['laydate', 'form'],
      function() {
        var form = layui.form;
        //渲染支持球队下拉框
        form.render("select");
        form.on('select', function(data) {
          teamId = data.value;
          // console.log(teamId);
        });
        //渲染地区下拉框
        form.render("selectAddress");
        form.on('select(address)', function(data) {
          // console.log(data.value);
          // console.log(teamId);
        });

        //性别单选框
        form.on('radio(demo-radio-filter)', function(data) {
          var elem = data.elem; // 获得 radio 原始 DOM 对象
          var value = +elem.value; // 获得 radio 值
          // console.log(value);
        });

        //日期选择器
        var laydate = layui.laydate;
        // 渲染
        laydate.render({
          elem: '#date'
        });
      });
  </script>

  <?php
  if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $userName = $_POST['userName'];
    $userPassword = $_POST['userPassword'];
    $nikeName = $_POST['nikeName'];
    $teamId = $_POST['teamId'];
    $gender = $_POST['gender'];
    $birthday = $_POST['birthday'];
    $age = $_POST['age'];
    $phone = $_POST['phone'];
    $address = $_POST['address'];

    $sqlAdd = "insert into user(user_name,user_password,nike_name,vote_team,gender,age,birthday,phone,address,createdBy) values('{$userName}','{$userPassword}','{$nikeName}',$teamId,$gender,$age,'{$birthday}','{$phone}','{$address}',$user_id);";
    $resAdd = mysqli_query($dbc, $sqlAdd);
    echo '<script>';
    if ($resAdd == 1) {
      echo '
        layui.use(\'layer\', function() {
          var layer = layui.layer;
          layer.msg(\'添加用户成功\');
          setTimeout(() => {
            var index = parent.layer.getFrameIndex(window.name); // 获取弹出层索引
            parent.layer.close(index); // 关闭弹出层
          }, 2000);
        });
      ';
    } else {
      echo '
        layui.use(\'layer\', function() {
          var layer = layui.layer;
          layer.msg(\'添加用户失败,请检查参数\');
          setTimeout(() => {
            var index = parent.layer.getFrameIndex(window.name); // 获取弹出层索引
            parent.layer.close(index); // 关闭弹出层
          }, 2000);
        });      
      ';
    }
    echo '</script>';
  }
  ?>
</body>

</html>